﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using Bricks.Component.Utils;
using Bricks.Business.Models.ProphaseManagement;
using Bricks.Business.Models.ProjectContract;
using Bricks.Site.Models;
using Bricks.Business.Data.Repositories.ProphaseManagement;
using Bricks.Business.Data.Repositories.ProjectContract;
using Bricks.Business.Service;
using Bricks.Business.Data.Repositories.Document;
using Bricks.Business.Models.Document;
using Bricks.Business.Data.Repositories.Account;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(IProphaseFileModifyInfoService))]
    internal class ProphaseFileModifyInfoService : IProphaseFileModifyInfoService
    {
        [Import]
        protected IProphaseFileModifyInfoRepository ProphaseFileModifyInfoRepository { get; set; }

        public List<ProphaseFileModifyInfoView> GetProphaseFileModifyInfoList(Guid pid)
        {
            IQueryable<ProphaseFileModifyInfo> prophaseFileModifyInfos = ProphaseFileModifyInfoRepository.Entities.Where(a => a.PID == pid);
            List<ProphaseFileModifyInfoView> lstProphaseFileModifyInfoView = new List<ProphaseFileModifyInfoView>();
            foreach (ProphaseFileModifyInfo prophaseFileModifyInfo in prophaseFileModifyInfos)
            {
                lstProphaseFileModifyInfoView.Add(new ProphaseFileModifyInfoView()
                {
                    ID = prophaseFileModifyInfo.ID,
                    PID = prophaseFileModifyInfo.PID,
                    MidifyTime = prophaseFileModifyInfo.MidifyTime.ToString("yyyy-MM-dd"),
                    MidifyReason = prophaseFileModifyInfo.MidifyReason
                });
            }
            return lstProphaseFileModifyInfoView;
        }

        public void SaveProphaseFileModifyInfo(Guid pid, List<ProphaseFileModifyInfoView> lstProphaseFileModifyInfoView)
        {

            DeleteAll(pid);
            foreach (ProphaseFileModifyInfoView prophaseFileModifyInfoView in lstProphaseFileModifyInfoView)
            {
                Insert(prophaseFileModifyInfoView);
            }
        }

        private void DeleteAll(Guid pid)
        {
            IQueryable<ProphaseFileModifyInfo> prophaseFileModifyInfos = ProphaseFileModifyInfoRepository.Entities.Where(a => a.PID == pid);
            List<Guid> lstDeleteID = new List<Guid>();
            foreach (ProphaseFileModifyInfo prophaseFileModifyInfo in prophaseFileModifyInfos)
            {
                lstDeleteID.Add(prophaseFileModifyInfo.ID);
            }
            foreach (Guid id in lstDeleteID)
            {
                ProphaseFileModifyInfoRepository.Delete(id);
            }
        }

        private void Insert(ProphaseFileModifyInfoView prophaseFileModifyInfoView)
        {
            ProphaseFileModifyInfo record = new ProphaseFileModifyInfo()
            {
                ID = prophaseFileModifyInfoView.ID,
                PID = prophaseFileModifyInfoView.PID,
                MidifyTime =Convert.ToDateTime(prophaseFileModifyInfoView.MidifyTime),
                MidifyReason = prophaseFileModifyInfoView.MidifyReason
            };
            ProphaseFileModifyInfoRepository.Insert(record);
        }
    }
}